//
//  FZEmoticonPageCell.swift
//  FZSina_Swift
//
//  Created by 李忠 on 7/30/16.
//  Copyright © 2016 FZLZ. All rights reserved.
//

import UIKit

class FZEmoticonPageCell: UICollectionViewCell {
    
    /// 表情按钮数组
    lazy var emoticonBtns:[FZEmoticonButton] = [FZEmoticonButton]()
    /// 表情数组，从collectionview传递过来
    var emoticons: [FZEmoticonModel]?{
        didSet{
            
            for value in emoticonBtns {
                
                value.hidden = true
                
            }
            
            for (i, emoticon) in emoticons!.enumerate() {
                
                
                let btn = emoticonBtns[i]
                btn.emoticon = emoticon
                
                btn.hidden = false
                if emoticon.isEmoji {
    
                    let title = (emoticon.code ?? "" as NSString).emoji()
                    btn.setTitle(title as String, forState: UIControlState.Normal)
                    btn.setImage(nil, forState: UIControlState.Normal)
                }else{
                   
                    let image = UIImage.getEmoticonImage(emoticon.path!)
                    btn.setTitle(nil, forState: UIControlState.Normal)
                    btn.setImage(image, forState: UIControlState.Normal)
                }
                
            }
            
        }
    }
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setupUI() {
        addChildButton()
        contentView.addSubview(deleteButton)
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
        let btnW = (self.frame.width-10) / CGFloat(FZEmoticonMaxCol)
        let btnH = (self.frame.height-20) / CGFloat(FZEmoticonMaxRow)
        
        for (i, btn) in emoticonBtns.enumerate() {
            /// 列
            let row = CGFloat(i % FZEmoticonMaxCol)
            /// 行
            let col = CGFloat(i / FZEmoticonMaxCol)
            let x = row * btnW+5
            let y = col * btnH
            btn.frame = CGRect(x: x, y: y, width: btnW, height: btnH)
        }
        deleteButton.frame = CGRect(x: self.frame.width - 5 - btnW, y: self.frame.height - 20 - btnH, width: btnW, height: btnH)
    }
    //删除操作的事件
    func deleteClickButton(){
        NSNotificationCenter.defaultCenter().postNotificationName(FZDELETEEMOTICONBUTTONNOTI, object: nil)
    }
    //MARK: 输入表情的方法
    func inputEmoticon(sender: FZEmoticonButton) {
        NSNotificationCenter.defaultCenter().postNotificationName(FZINPUTEMOTICONNOTI, object: sender.emoticon)
    }
    
    //添加按钮的方法
    func addChildButton() {
        
        for _ in 0..<FZEmoticonMaxCount {
            
            let btn = FZEmoticonButton()
            
            btn.addTarget(self, action: #selector(inputEmoticon(_:)), forControlEvents: UIControlEvents.TouchUpInside)
            btn.titleLabel?.font = UIFont.systemFontOfSize(30)
            btn.backgroundColor = RANDRGB()
            contentView.addSubview(btn)
            emoticonBtns.append(btn)
        }
    }
    //MARK: 删除按钮
    lazy var deleteButton: UIButton = {
       
        let btn = UIButton()
        btn.addTarget(self, action: #selector(deleteClickButton), forControlEvents: UIControlEvents.TouchUpInside)
        btn.backgroundColor = self.backgroundColor
        btn.setImage(UIImage(named: "compose_emotion_delete"), forState: UIControlState.Normal)
        btn.setImage(UIImage(named: "compose_emotion_delete_highlighted"), forState: UIControlState.Highlighted)
        return btn
    }()
}
